<!-- 添加角色 -->
<template>
  <div class="role-add">
    <div class="header">添加角色</div>
    <el-form ref="roleForm" :model="roleForm" label-width="120px" :rules="rules">
      <el-form-item label="角色名称:" prop="role_name">
        <el-input v-model="roleForm.role_name" placeholder="请输入角色名称"></el-input>
      </el-form-item>
      <el-form-item label="角色描述:">
        <el-input v-model="roleForm.desc" type="textarea" placeholder="请给角色添加一些描述~~~"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addRoleEvent">立即添加</el-button>
        <el-button @click="$router.back()">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  inject: ["reload"],
  data() {
    return {
      roleForm: {
        role_name: ""
      },
      rules: {
        // 表单验证规则
        role_name: [
          {
            required: true,
            message: "请输入角色名称",
            trigger: "blur"
          },
          {
            min: 2,
            max: 10,
            message: "角色名称长度在 2 到 10 个字符",
            trigger: "blur"
          }
        ]
      }
    };
  },
  created() {},
  methods: {
    addRoleEvent() {
      this.$refs.roleForm.validate(valid => {
        if (valid) {
          //添加
          this.$http.addRole(this.roleForm).then(res => {
            if (res && res.data.code === 200) {
              this.$message({
                type: "success",
                message: res.data.msg
              });
              this.$router.push("/dashboard/system/role");
              this.reload();
            }
          });
        }
      });
    }
  }
};
</script>

<style lang="less">
.role-add {
  width: 100%;
  background: #fff;
  .header {
    padding: 15px 0 15px 20px;
    border-bottom: 1px solid #eee;
  }
  .el-form {
    padding: 40px 20px 80px 20px;
    width: 70%;
    margin: 0 auto;
  }
}
</style>
